package com.jmemoir.base.common.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * 全局响应结果
 *
 * @author Tellsea
 * @date 2023/8/1
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    private Integer code;

    private boolean success;

    private String msg;

    private T data;

    public static <T> Result<T> success() {
        return (Result<T>) Result.builder().code(HttpStatus.OK.value()).success(true).msg("操作成功").data(null).build();
    }

    public static <T> Result<T> success(String msg) {
        return (Result<T>) Result.builder().code(HttpStatus.OK.value()).success(true).msg(msg).data(null).build();
    }

    public static <T> Result<T> success(String msg, T data) {
        return (Result<T>) Result.builder().code(HttpStatus.OK.value()).success(true).msg(msg).data(data).build();
    }

    public static <T> Result<T> fail() {
        return (Result<T>) Result.builder().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).success(false).msg("操作失败").data(null).build();
    }

    public static <T> Result<T> fail(String msg) {
        return (Result<T>) Result.builder().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).success(false).msg(msg).data(null).build();
    }

    public static <T> Result<T> fail(String msg, T data) {
        return (Result<T>) Result.builder().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).success(false).msg(msg).data(data).build();
    }

    public static <T> Result<T> fail(Integer code, String msg) {
        return (Result<T>) Result.builder().code(code).success(false).msg(msg).data(null).build();
    }

    public static <T> Result<T> fail(Integer code, String msg, T data) {
        return (Result<T>) Result.builder().code(code).success(false).msg(msg).data(data).build();
    }
}
